﻿using BarManager.Core.BusinessRules.Interfaces.Specification;
using BarManager.Core.DomainService;
using BarManager.Core.Entity;
using System.Collections.Generic;

namespace BarManager.Core.BusinessRules.Specification
{
    public class CategoriaDeleteSpecification : BaseSpecification<Categoria>, ICategoriaSpecification
    {
        private Categoria Categoria = null;
        private int CountProductos = 0;

        public CategoriaDeleteSpecification(ICategoriaRepository repository)
            : base(repository)
        {
        }

        public override bool IsSatisfiedBy(Categoria entity)
        {
            Categoria = BaseRepository.Get(x => x.Id == entity.Id);
            if (Categoria != null && Categoria.ProductoList != null)
                CountProductos = Categoria.ProductoList.Count;

            return (CountProductos == 0);
        }

        public override IEnumerable<string> GetErrorMessage()
        {
            if (CountProductos > 0) yield return "La categoria no puede ser eliminada porque está asignada a productos";
            yield break;
        }
    }
}
